﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.Variables;
using CRMG.UserManagement;
using CRMG.UserManagement.Source.BO;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using System;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Windows.Forms;

namespace CRMG.Container.Views.Login
{

    public partial class Login : Telerik.WinControls.UI.RadForm
    {
        UserManager userMan;
        ResourceManager formRes;
        ResourceManager messageRes;
        public Login()
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);

                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(Login).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(Login).Assembly);
                userMan = new UserManager();
                lblUser.Text = formRes.GetString("Login.lblUser");
                lblPass.Text = formRes.GetString("Login.lblPass");
                btnLogin.Text = formRes.GetString("Login.btnLogin");
                lblErrorMessage.Text = formRes.GetString("Login.lblError");
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry()
                {
                    Title = Rc.GetString(2000),
                    ErrorCode = 2000,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("Login.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
            finally
            {
                txtUser.Focus();
            }
        }



        private void LoginClick(object sender, EventArgs e)
        {
            try
            {
                User usr = userMan.GetUser(txtUser.Text, SecurityUtil.Encode(txtPass.Text));
                AppVariables.CurrentUser = usr;
                Program.isLogged = true;
                this.Close();
            }
            catch (Exception)
            {
                lblErrorMessage.Visible = true;
                txtUser.ResetText();
                txtPass.ResetText();
                txtUser.Focus();
            }
        }

        private void Exit(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                System.Windows.Forms.DialogResult dr = ControlUtil.ShowConfirmDialog(
                    messageRes.GetString("Login.Exit.Confirm"),
                    messageRes.GetString("Warning"));
                if (dr == System.Windows.Forms.DialogResult.Yes)
                {
                    Application.Exit();
                }
            }
        }
    }
}
